package com.itheima.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Service.SetmealService;
import com.itheima.reggie.Sharing.SetmalDto;
import com.itheima.reggie.exception.R;
import com.itheima.reggie.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String > save(@RequestBody SetmalDto dto)
    {
        setmealService.saveWith(dto);
        return R.success("保存成功");
    }
    @GetMapping("/page")
    public  R<Page> page(int page,int pageSize,String name)
    {
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByAsc("update_time");
        if(name!=null) {
            wrapper.like("name", name);
        }
        setmealService.page(pageInfo,wrapper);
        return R.success(pageInfo);

    }
    @DeleteMapping
    public R<String> Delete(@RequestParam List<Long> ids)
    {
           setmealService.removeByIds(ids);
           return R.success("删除成功");
    }
    @GetMapping("/{id}")
    public R<SetmalDto> huiX(@PathVariable Long id)
    {

  SetmalDto dto=setmealService.Huixian(id);
     return R.success(dto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmalDto dto)
    {
        setmealService.update2(dto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal)
    {
        QueryWrapper<Setmeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",setmeal.getCategoryId());
        queryWrapper.eq("status",setmeal.getStatus());
        List<Setmeal> list=setmealService.list(queryWrapper);
         return R.success(list);
    }
 }

